package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.myapplication.bean.Login;
import com.google.gson.Gson;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button btn_login;
    private EditText et_user_name1;
    private EditText et_user_name;
    private TextView forget_password;
    private TextView regist;
    private String username;
    private String passsword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();

    }

    private void initView() {
        et_user_name = findViewById(R.id.et_user_name);
        et_user_name1 = findViewById(R.id.et_user_name1);
        btn_login = findViewById(R.id.btn_login);
        forget_password = findViewById(R.id.forget_password);
        regist = findViewById(R.id.regist);
        btn_login.setOnClickListener(this);

        username = et_user_name.getText().toString().trim();
        passsword = et_user_name1.getText().toString().trim();
    }

    private void initData() {
        //String url = "http://anli-wechat.boruiman.com/freshmenapp/user/login";
        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpClient client = new OkHttpClient();
                RequestBody formBody = new FormBody.Builder()
                        .add("name",username)
                        .add("password",passsword)
                        .build();
                final Request request= new Request.Builder()
                        .url("http://anli-wechat.boruiman.com/WorldCupApp/user/login")
                        .post(formBody)
                        .build();
                client.newCall(request).enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {

                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        if (!response.isSuccessful()){
                            Toast.makeText(MainActivity.this, "请求失败", Toast.LENGTH_SHORT).show();
                        }
                        Gson gson = new Gson();
                        final Login login = gson.fromJson(response.body().string(), Login.class);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(MainActivity.this, "登录"+login.getMsg(), Toast.LENGTH_SHORT).show();
                            }
                        });
                    }
                });
            }
        }).start();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_login:
                initData();
                break;
        }
    }
}